<template>
  
    
    <el-container class="home-container">
      <el-header>
          <div>
            <el-tooltip class="item" effect="dark" content="退出登录" placement="right">
              <img src="../assets/preview.gif" alt="" @click="logout">
            </el-tooltip>
            <span>销售管理系统</span>&nbsp;
            
          </div>
        </el-header>
        
      <el-container>
        <!-- 左部导航栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollappse">
            <i style="font-size:28px"  v-show="isCollapse" class="el-icon-s-unfold"></i>
            <i style="font-size:28px"  v-show="! isCollapse" class="el-icon-s-fold"></i>
          </div>
           <el-menu
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            :unique-opened="true"
            :collapse="isCollapse"
            :router="true"
            >
            <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
             <!-- 一级菜单 -->
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item 
              :index="'/' + subItem.path" 
              v-for="subItem in item.children" 
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
              
            </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容区 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  
</template>

<script>
export default {
  data(){
    return{
      isCollapse: false,
      //左侧菜单
      menulist:[],
      iconsObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
    }
   
  }, 
  created(){
    this.getMenuList()
  },
  methods:{
    //点击菜单的折叠与展开
    toggleCollappse(){
      this.isCollapse = !this.isCollapse
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
      //退出
    logout(){
      window.sessionStorage.clear();
      this.$router.push('/login')
    },
    async getMenuList(){
      const {data:res,request:request} = await this.$http.get('menus')
      console.log(res.data);
        if (request.readyState===4) {
              if(res.meta.status>=200&&res.meta.status<300){
                 this.menulist = res.data
              }else{
                this.$notify.error({
                  title: res.meta.msg,
                  message: '获取数据失败，请检查网络'
                });
              }
            }else{
              this.$message.error("readyState状态错误")
            }
    }
  }
}
</script>

<style lang="less" scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: rgb(179,192,209);
  padding-left: 0;
  >div{
    display: flex;
    align-items: center;
    img{
      width: 60px;
      border-radius: 50%;
      
    }
    span{
      margin-left: 10px;
    }
  }
}
.toggle-button{
  position: absolute;
  left: 200px;
  top: 15px;
  cursor: pointer;
}
.el-aside{
  background-color: rgb(84,92,100);
  .el-menu {
    //左侧菜单栏右边框不对齐
    border-right: none;
  }
}
.el-main{
  background-color: rgb(233,238,243);
}
.iconfont {
  margin-right: 10px;
}

//左侧菜单栏折叠
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
</style>